package com.td.lib.viewmodel;

import java.util.HashMap;

/**
 * ViewModelStore
 */
public class ViewModelStore {
    private final HashMap<String, ViewModel> mHashMap = new HashMap<>();

    final void put(String key, ViewModel viewModel) {
        ViewModel oldViewModel = mHashMap.put(key, viewModel);
        if (oldViewModel != null) {
            oldViewModel.onCleared();
        }
    }

    final ViewModel get(String key) {
        return mHashMap.get(key);
    }

    /**
     * 清除缓存数据
     */
    public final void clear() {
        for (ViewModel vm : mHashMap.values()) {
            vm.clear();
        }
        mHashMap.clear();
    }
}
